package com.anrongtec.zcpt.tab04;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.base.BaseFragment;
import com.anrongtec.zcpt.base.DDApp;
import com.anrongtec.zcpt.constant.PreferenceKey;
import com.anrongtec.zcpt.manager.APPDownloadManager;
import com.anrongtec.zcpt.manager.AppDownManagerActivity;
import com.anrongtec.zcpt.manager.AppUninstallActivity;
import com.anrongtec.zcpt.manager.AppUpgradeActivity;
import com.anrongtec.zcpt.manager.BadgeViewManager;
import com.anrongtec.zcpt.manager.MyStatisticalActivity;
import com.anrongtec.zcpt.manager.UninstallActivity;
import com.anrongtec.zcpt.module.FunctionModel;
import com.anrongtec.zcpt.service.AppUpgradeService;
import com.anrongtec.zcpt.view.BadgeView;
import com.art.base.util.DialogUtil;
import com.art.base.util.FileSizeUtil;
import com.art.base.util.PreferenceUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter.OnItemClickListener;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * tab4 me
 * 
 * @author cxy
 *
 */
public class TabFourthFragment extends BaseFragment implements OnClickListener {

	private RecyclerView rv;
	private TextView tv_header, tv_username;
	private ImageView iv_setting;
	private List<FunctionModel> functions;

	private CommonAdapter<FunctionModel> adapter;

	private BadgeView badge;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_tab_fourth, null);

		initData();

		initViews(v);

		showBadge();

		return v;
	}

	@Override
	protected void initData() {
		functions = new ArrayList<FunctionModel>();
		functions.add(new FunctionModel(R.drawable.icon_tab04_update, "应用升级", null));
		functions.add(new FunctionModel(R.drawable.icon_tab04_down, "应用下载", null));
		functions.add(new FunctionModel(R.drawable.icon_tab04_uninst, "应用卸载", null));
		// functions.add(new FunctionModel(R.drawable.icon_tab04_list, "我的统计",
		// null));
		functions.add(new FunctionModel(R.drawable.icon_tab04_clean_cache, "清除缓存", null));
		functions.add(new FunctionModel(R.drawable.icon_tab04_suggestion, "意见反馈", null));
		functions.add(new FunctionModel(R.drawable.icon_tab04_about, "关于", null));
	}
	@Override
	protected void initViews(View view) {
		iv_setting = (ImageView) view.findViewById(R.id.iv_tab04_setting);
		iv_setting.setOnClickListener(this);

		tv_username = (TextView) view.findViewById(R.id.tv_tab04_username);
		//显示用户名
		tv_username.setText(DDApp.dApp.getCurrentUser().getUserName());
		//一开始登录的时候存储了身份证号，身份证号为ACCOUNT_ID
		((TextView) view.findViewById(R.id.tv_tab04_useridno))
				.setText(PreferenceUtils.getInstance(getActivity()).getString(PreferenceKey.ACCOUNT_ID));
		rv = (RecyclerView) view.findViewById(R.id.recyclerview_tab04);
		rv.addItemDecoration(new DividerGridItemDecoration(getActivity()));
		rv.setLayoutManager(new GridLayoutManager(getActivity(), 3,
				GridLayoutManager.VERTICAL, false));

		adapter = new CommonAdapter<FunctionModel>(getContext(), R.layout.item_tab_fourth, functions) {

			@Override
			protected void convert(ViewHolder holder, FunctionModel t, int position) {
				BadgeViewManager.getInstance(getActivity()).setBadge(holder.getView(R.id.iv_tab04_item),
						t.getBadgeCount());
				holder.setImageResource(R.id.iv_tab04_item, t.getLocalIcon());
				holder.setText(R.id.tv_tab04_item, t.getName());
			}
		};
		adapter.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
				switch (functions.get(position).getName()) {
				case "应用升级":
					startActivity(AppUpgradeActivity.class);
					break;
				case "应用下载":
					startActivity(AppDownManagerActivity.class);
					break;
				case "应用卸载":
					startActivity(AppUninstallActivity.class);
//					startActivity(UninstallActivity.class);
					break;
				case "我的统计":
					startActivity(MyStatisticalActivity.class);
					break;
				case "清除缓存":
					setCleanCache();
					break;
				case "意见反馈":
					// 意见反馈
					intent = new Intent(getActivity(), SuggestionFeedbackActivity.class);
					startActivity(intent);
					break;
				case "关于":
					intent = new Intent(getActivity(), AboutActivity.class);
					startActivity(intent);
					break;
				default:
					break;
				}
			}

			@Override
			public boolean onItemLongClick(View view, android.support.v7.widget.RecyclerView.ViewHolder holder,
					int position) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		rv.setAdapter(adapter);
	}

	/**
	 * 清空缓存点击事件
	 */
	private void setCleanCache() {

		DialogUtil.createAlertDialog(getActivity(), "清除缓存", 0,
				"是否清除缓存" + FileSizeUtil.getAutoFileOrFilesSize(
						ImageLoader.getInstance().getDiskCache().getDirectory().getAbsolutePath()) + "？",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 清除缓存
						ImageLoader.getInstance().getDiskCache().clear();
					}
				});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_tab04_setting:
			startActivity(SettingActivity.class);
			break;

		default:
			break;
		}

	}

	/**
	 * 显示badge
	 */
	private void showBadge() {
		setBadge();
	}

	/**
	 * 应用升级模块显示的红点
	 * 
	 * @param
	 *            i=0 则不显示红点
	 */
	public void setBadge() {
		// 应用升级
		functions.get(0).setBadgeCount(AppUpgradeService.getNeedUpgreade().size());
		// 应用下载
		functions.get(1).setBadgeCount(APPDownloadManager.getDownloadTaskCount());
		adapter.notifyDataSetChanged();
	}

}
